package com.ku.order.controller;

import com.ku.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: rocketMQKill
 * @description:
 * @author: JokerKu
 * @create: 2020-05-04 16:54
 **/

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @RequestMapping("/{name}")
    public String hello(@PathVariable String name){
        String test = orderService.test(name);
        return test;
    }

    //	超时降级
	@HystrixCommand(
				commandKey = "createOrder",
				commandProperties = {
						@HystrixProperty(name="execution.timeout.enabled", value="true"),//开启超时降级
						@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="3000"),//设定3s为降级时间点
				},
				fallbackMethod = "createOrderFallbackMethod4Timeout"
			)
    @RequestMapping("/createOrder")
    public String createOrder(@RequestParam("cityId")String cityId,
                              @RequestParam("platformId")String platformId,
                              @RequestParam("userId")String userId,
                              @RequestParam("supplierId")String supplierId,
                              @RequestParam("goodsId")String goodsId) throws Exception {

        return orderService.createOrder(cityId, platformId, userId, supplierId, goodsId) ? "下单成功!" : "下单失败!";
    }
    //超时降级方法
    public String createOrderFallbackMethod4Timeout(@RequestParam("cityId")String cityId,
                                                    @RequestParam("platformId")String platformId,
                                                    @RequestParam("userId")String userId,
                                                    @RequestParam("suppliedId")String suppliedId,
                                                    @RequestParam("goodsId")String goodsId) throws Exception {
        System.err.println("-------超时降级策略执行------------");
        return "hysrtix timeout !";
    }
}
